package yang.itcode.service.impl;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yang.itcode.exception.BusinessException;
import yang.itcode.mapper.SupplierMapper;
import yang.itcode.model.pojo.Supplier;
import yang.itcode.service.SupplierService;
import yang.itcode.utils.ThreadUtil;

import static yang.itcode.constant.ErrorConstant.*;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public boolean addSupplier(JSONObject obj) {
        String name = obj.getStr("name");
        String contact = obj.getStr("contact");
        String phone = obj.getStr("phone");
        String email = obj.getStr("email");
        String address = obj.getStr("address");
        Integer userId = ThreadUtil.getCurrentId();
        if (name == null) {
            throw new BusinessException(SUPPLIER_PARAM_EXCEPTION_CODE, SUPPLIER_PARAM_EXCEPTION_MESSAGE);
        }
        Supplier supplier = new Supplier();
        supplier.setName(name);
        supplier.setContact(contact);
        supplier.setPhone(phone);
        supplier.setEmail(email);
        supplier.setAddress(address);
        supplier.setCreateUserId(userId);
        supplier.setUpdateUserId(userId);
        return supplierMapper.insert(supplier) == 1;
    }

    @Override
    public PageInfo<JSONObject> getSupplierList(Integer pageNum, Integer pageSize, JSONObject condition) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        condition.set("status", 1);
        return new PageInfo<>(supplierMapper.selectSupplierView(condition));
    }

    @Override
    public JSONObject getSupplierDetail(Integer id) {
        Supplier supplier = supplierMapper.selectById(id);
        if (supplier == null || supplier.getStatus() != 1) {
            throw new BusinessException(SUPPLIER_NOT_EXIST_EXCEPTION_CODE, SUPPLIER_NOT_EXIST_EXCEPTION_MESSAGE);
        }
        JSONObject obj = new JSONObject();
        obj.set("id", supplier.getId());
        obj.set("name", supplier.getName());
        obj.set("contact", supplier.getContact());
        obj.set("phone", supplier.getPhone());
        obj.set("email", supplier.getEmail());
        obj.set("address", supplier.getAddress());
        return obj;
    }

    @Override
    public boolean updateSupplier(JSONObject obj) {
        Integer id = obj.getInt("id");
        String name = obj.getStr("name");
        String contact = obj.getStr("contact");
        String phone = obj.getStr("phone");
        String email = obj.getStr("email");
        String address = obj.getStr("address");
        if (name == null || id == null) {
            throw new BusinessException(SUPPLIER_PARAM_EXCEPTION_CODE, SUPPLIER_PARAM_EXCEPTION_MESSAGE);
        }
        Integer userId = ThreadUtil.getCurrentId();
        Supplier supplier = supplierMapper.selectById(id);
        supplier.setName(name);
        supplier.setContact(contact);
        supplier.setPhone(phone);
        supplier.setEmail(email);
        supplier.setAddress(address);
        supplier.setUpdateUserId(userId);
        return supplierMapper.update(supplier) == 1;
    }

    @Override
    public boolean deleteSupplier(Integer id) {
        Integer userId = ThreadUtil.getCurrentId();
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplier.setStatus(0);
        supplier.setUpdateUserId(userId);
        return supplierMapper.update(supplier) == 1;
    }
}
